04. Calibration Pattern

Calibration Pattern

To calibrate your camera you need to measure how 3D points in the world get mapped down onto the 2D image plane in the camera. In reality, that's a rather complicated process, but in these next exercises I'll show you how to make it easy using some powerful OpenCV tools!

The first step is to choose a calibration pattern. In these exercises we'll be using this chessboard pattern:

A chessboard is not the only option for a test pattern, it could be a pattern made up of some other simple shapes like circles. Whatever shape you use, the purpose of the test pattern is to provide a known geometry that you can take pictures of using your camera, and use as a reference when mapping 3D world points to 2D image points.

Take Pictures of the Test Pattern

The next step is to print out the test pattern and take pictures of it! You can either fix the position of the camera and move the test pattern around in the scene, or fix the test pattern in the scene and move the camera around to take pictures from different angles and distances. Here I printed out the test pattern so each square is 10cm x 10cm and stuck it to a wall. I then took pictures from various aspects that look like this:

Find Corners

Once you have images of your test pattern (in this case they'll be provided to you), you first need to locate the test pattern in each image.

In the next exercise, you'll use the OpenCV functions findChessboardCorners() and drawChessboardCorners() to automatically find and draw the "inner corners" on your images of the chessboard pattern.

To learn more about both of those functions, you can have a look at the OpenCV documentation here: cv2.findChessboardCorners() and cv2.drawChessboardCorners().

Applying these two functions to a sample image, you'll get a result like this:

In the exercise below, your task is simple. Count the number of inside corners in any given row and enter that value in the variable nx. Similarly, count the number of corners in a given column and store that in ny. Keep in mind that inside corners are only points where two black and two white squares intersect, in other words, only count inside corners, not outside corners.

Start Quiz:

import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob

# prepare object points
nx = 0#TODO: enter the number of inside corners in x
ny = 0#TODO: enter the number of inside corners in y

# Make a list of calibration images
images = glob.glob('./Cal*.jpg')
# Select any index to grab an image from the list
idx = -1
# Read in the image
img = mpimg.imread(images[idx])

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None)

# If found, draw corners
if ret == True:
    # Draw and display the corners
    cv2.drawChessboardCorners(img, (nx, ny), corners, ret)
    plt.imshow(img)

User's Answer:

(Note: The answer done by the user is not guaranteed to be correct)

import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob

# prepare object points

nx = 8#TODO: enter the number of inside corners in x
ny = 6#TODO: enter the number of inside corners in y

# Make a list of calibration images
images = glob.glob('./Cal*.jpg')
# Select any index to grab an image from the list
idx = -1
# Read in the image
img = mpimg.imread(images[idx])

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None)

# If found, draw corners
if ret == True:
    # Draw and display the corners
    cv2.drawChessboardCorners(img, (nx, ny), corners, ret)
    plt.imshow(img)